﻿using MC.Warehouse.Core;
using MC.Warehouse.Models;
using MC.Warehouse.DAL;
using System.Windows;

namespace MC.Warehouse.ViewModels
{
	public class CustomerViewModel : UserControlViewModel
    {
        #region CONTRUCTOR
        
        public CustomerViewModel()
		{
            Initialize();
		}

        #endregion

        #region USERCONTROL CORE FUNCTION

        internal override sealed void Initialize()
        {
            Model = new CustomerModel();
        }

        internal override void Save()
        {
            if (MessageBoxResult.No == MessageBox.Show("Do you want to save?", "System",
                MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes))
                return;

            byte flag;
            using (var repository = new CustomersRepository())
            {
                flag = string.IsNullOrEmpty(_model.Id) ? repository.Insert(_model) : repository.Update(_model);
            }
            if (flag == 1)
                MessageBox.Show("Successfull.", "System",
                MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
        }

        internal override void Refresh()
        {
            if (string.IsNullOrEmpty(_model.Id))
                return;
            using (var repository = new CustomersRepository())
            {
                Model = repository.FindById(_model.Id);
            }
        }

        internal override void Clean()
        {
            Model = new CustomerModel();
        }

        internal override void New()
        {
            Model = new CustomerModel();
        }

        internal override void Copy()
        {
            _model.Id = string.Empty;
        }

        internal override void Delete()
        {
            if (MessageBoxResult.No == MessageBox.Show("Do you want to delete?", "System",
                MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No))
                return;
            byte flag;
            using (var repository = new CustomersRepository())
            {
                flag = repository.Delete(_model.Id);
            }
            if (flag == 1)
            {
                Clean();
                MessageBox.Show("Successfull.", "System",
                MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
            }
        }

        internal override void Search()
        {
            TabbarManager.OpenTab(UcPartialName, UcPartialCaption);
        }

        internal override void Print()
        {
            
        }

        #endregion
        
        #region PROPERTIES AND DATA FIELD

        private const string UcPartialName = "CustomerListView";
        private const string UcPartialCaption = "Customer list";
        private CustomerModel _model;

        public override BaseModel Model
        {
            get { return _model ?? (_model = new CustomerModel()); }
            set
            {
                _model = value as CustomerModel;
                NotifyPropertyChanged(()=>Model);
            }
        }

        #endregion
    }
}